package com.caac.core.web.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ac.app.web.view.PkRecordView;
import com.caac.core.Global;
import com.caac.core.util.HttpUtils;
import com.caac.core.util.Page;
import com.caac.core.web.form.BaseForm;
import com.caac.core.web.form.BaseSearchForm;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright caac 2004</p>
 * <p>Company: caac</p>
 * @author <a href="mailto:project@caac.com.cn">Project Group</a>
 * @version 1.0
 */

public abstract class BasePageAction extends BaseAction {
    protected final ActionForward doExecute(ActionMapping mapping,
                                            BaseForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response) throws
            Exception {
        BaseSearchForm myForm  = (BaseSearchForm) form;

        PageForward pf = doPageExecute(mapping, form, request, response,
                myForm.getPageNo(),  myForm.getPageSize());
        
        HttpUtils.setAttribute(request, Global.KEY_DATA, pf.getData());
        HttpUtils.setAttribute(request, Global.KEY_PAGE, pf.getPage());
        return pf.getForward();
    }

    protected abstract PageForward doPageExecute(ActionMapping mapping,
                                                 BaseForm form,
                                                 HttpServletRequest request,
                                                 HttpServletResponse response,
                                                 int pageNo, int pageSize) throws
            Exception;

    private int getPageNo(HttpServletRequest request) {
        return HttpUtils.getIntParameter(request, Global.KEY_PAGE_NO,
                                         Page.DEFAULT_PAGE_NO);
    }

    private int getPageSize(HttpServletRequest request) {
        return HttpUtils.getIntParameter(request, Global.KEY_PAGE_SIZE,
                                         Page.DEFAULT_PAGE_SIZE);
    }
}
